const Base = require("../base.js");
const txyoss = require("../public/txyoss");
const puppeteer = require("puppeteer");
module.exports = class extends Base {
    async indexAction() {
        const browser = await puppeteer.launch({
            args: ["--no-sandbox", "--disable-setuid-sandbox"],
        });
        const page = await browser.newPage();

       // 设置页面大小
  await page.setViewport({ width: 1200, height: 800 });

  // 注入新的CSS样式
  await page.evaluate(() => {
    const style = document.createElement('style');
    style.textContent = `
      @media print {
        body {
          width: 800px;
          height: 1050px;
        }
      }
    `;
    document.head.appendChild(style);
  });
  

        await page.goto(this.get("url"), {
            //这个可以渲染出图片甚至跨域的图片
            waitUntil: "networkidle2",
        });
        const pdf =  await page.pdf({ path: 'example.pdf', format: 'A4', landscape: this.get("landscape") ? true : false });

        let imgurl = await txyoss.upbuff(pdf, "yspdf", ".pdf");
        await browser.close();
        this.success(imgurl);
    }
};
